/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import entities.Faq;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.aptech.session.beans.FaqFacade;

/**
 *
 * @author Chien
 */
@ManagedBean
@SessionScoped
public class FaqControl {
    @EJB
    private FaqFacade faqFacade;
    private Faq faq;
    private Faq selectedFaq;

    public Faq getFaq() {
        return faq;
    }

    public void setFaq(Faq faq) {
        this.faq = faq;
    }

    public Faq getSelectedFaq() {
        return selectedFaq;
    }

    public void setSelectedFaq(Faq selectedFaq) {
        this.selectedFaq = selectedFaq;
    }
    public List<Faq> findAll() {
        return faqFacade.findAll();
    }

    public Faq getdetail() {
        return faq;
    }

    public Faq selectedFaq() {
        return selectedFaq;
    }

    public void selectedFaq(Faq selectedFaq) {
        this.selectedFaq = selectedFaq;
    }

    /**
     * Creates a new instance of FaqControl
     */
    public FaqControl() {
        faq=new Faq();
        selectedFaq=new Faq();
    }
    public void showdetail(Faq faq) {
        this.faq = faq;
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/FaqUpdate.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void delete(Faq faq) {
        
            faqFacade.remove(faq);
            try{
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/FaqManager.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void update() {
        faqFacade.edit(faq);
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/FaqManager.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void insert(ActionEvent event) {
        
            faqFacade.create(faq);
            FacesContext context = FacesContext.getCurrentInstance();
         try{   
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/FaqManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(FaqControl.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }

    public void goInsert() {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/FaqInsert.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public void delete()
    {
        try {
            faqFacade.remove(faq);
            FacesContext context = FacesContext.getCurrentInstance();
                context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/FaqInsert.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(FaqControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void setFiled()
    {
        faq.setFaqId(Integer.parseInt(""));
        faq.setAsking("");
        faq.setAnswering("");
    }
}
